package com.csu.tour.controller;

import com.csu.tour.common.CommonResponse;
import com.csu.tour.entity.Complaint;
import com.csu.tour.entity.PageBean;
import com.csu.tour.service.ComplaintService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

@RestController
@Slf4j
public class ComplaintController {

    @Autowired
    ComplaintService complaintService;

    /**
     * 游客评价
     * @param complaintId
     * @param evaluation
     * @return
     */
    @PutMapping("/complaints/evaluation")
    public CommonResponse evaluationPost(@RequestParam int complaintId, @RequestParam String evaluation){
        log.info("投诉评价，参数：complaintId={},evaluation={}",complaintId,evaluation);
        complaintService.evaluation(complaintId,evaluation);
        return CommonResponse.createForSuccess();
    }

    /**
     * 管理员审批
     * @param complaintId
     * @param statusApprove
     * @return
     */
    @PutMapping ("/complaints/statusApprove")
    public CommonResponse statusApprove(@RequestParam int complaintId,
                                        @RequestParam Complaint.ComplaintStatusApprove statusApprove){
        log.info("投诉审批，参数：complaintId={},statusApprove={}",complaintId,statusApprove);
        complaintService.statusApprove(complaintId,statusApprove);
        return CommonResponse.createForSuccess();
    }

    /**
     * 添加投诉
     * @param complaint
     * @return
     */
    @PostMapping("/complaints")
    public CommonResponse complaintPost(@RequestBody Complaint complaint) {
        log.info("投诉添加，参数：{}",complaint);
        complaintService.complaintAdd(complaint);
        return CommonResponse.createForSuccess();
    }

    /**
     * 投诉处理
     * @param complaintId
     * @param handlingContent
     * @param status
     * @return
     */
    @PutMapping("/complaints/process")
    public CommonResponse process(@RequestParam int complaintId,@RequestParam String handlingContent,
                                  @RequestParam Complaint.ComplaintStatus status){
        log.info("投诉处理，参数：complaintId={},handlingContent={},status={}",complaintId,handlingContent,status);
        complaintService.process(complaintId,handlingContent,status);
        return CommonResponse.createForSuccess();
    }

    /**
     * 投诉结案
     * @param complaintId
     * @param statusApprove
     * @return
     */
    @PutMapping("/complaints/finish")
    public CommonResponse finish(@RequestParam int complaintId,
                                 @RequestParam Complaint.ComplaintStatusApprove statusApprove) {
        log.info("投诉完成，参数：complaintId={},statusApprove={}",complaintId,statusApprove);
        complaintService.finish(complaintId,statusApprove);
        return CommonResponse.createForSuccess();
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param status
     * @param begin
     * @param end
     * @return
     */
    @GetMapping("/complaints")
    public CommonResponse page(@RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer pageSize,
                               Complaint.ComplaintStatus status,
                               Complaint.ComplaintStatusApprove statusApprove,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                               @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end){
        log.info("分页条件查询，参数：page={},pageSize={},status={},statusApprove={},begin={},end={}",
                page,pageSize,status,statusApprove,begin,end);
        //调用service分页查询
        PageBean pageBean = complaintService.page(page,pageSize,status,statusApprove,begin,end);
        return CommonResponse.createForSuccess(pageBean);
    }
}
